package com.cxw.administrator.folderlist;

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/25.
 */
public class SDUtil {
    public static final String STORAGE_PATH = "storage/emulated/0";

    //    判断是否挂载SD卡
    public static boolean isMounted() {
//        Environment.DIRECTORY_DCIM 得到外部存储设备中DCIM的路径
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
//
    }

    //    找到SD卡的路径
    public static String getSDpath() {
        if (isMounted()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    //    计算SD卡的总大小
    public static long getSDTotalSize() {
        if (isMounted()) {
//            获取文件的大小

            StatFs statFs = new StatFs(getSDpath());
//            判断版本号是否大于18
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//              获取块的数量
//            直接方法statFs.getTotalBytes();
                long blockCount = statFs.getBlockCountLong();
//                获取每个块的大小
                long blockSize = statFs.getBlockSizeLong();
                return blockSize * blockCount / 1024 / 1024;
            }
        }
        return 0;
    }

    //    计算可用空间
    public static long getSDFreeSize() {
        if (isMounted()) {
//            获取文件的大小
            StatFs statFs = new StatFs(getSDpath());
//            判断版本号是否大于18
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//              获取块的数量
                long blockCount = statFs.getAvailableBlocksLong();
//                获取每个块的大小
                long blockSize = statFs.getBlockSizeLong();
////                直接获得大小的方法
//                statFs.getAvailableBytes();
                return blockSize * blockCount / 1024 / 1024;
            }
        }
        return 0;
    }

    public static boolean saveData(String fileName, byte[] data) {
        if (isMounted()) {
            String path = getSDpath() + File.separator + STORAGE_PATH;
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }

            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data);
                fos.flush();
                fos.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;

    }
    public static byte[] readFile(String fileName){
        if(isMounted()){
            String path=getSDpath()+File.separator+STORAGE_PATH;
            File file=new File(path);
            if(file.exists()){

                try {
                    ByteArrayOutputStream baos=new ByteArrayOutputStream();
                    FileInputStream fis=new FileInputStream(file);
                    int len=0;
                    byte [] buffer=new byte[1024];
                    while((len=fis.read(buffer))!= -1){
                        baos.write(buffer);
                        baos.flush();
                    }
                    baos.close();
                    fis.close();
                    return baos.toByteArray();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return null;
    }
}
